#include <iostream>
using namespace std;

// struct A
// {
//   long a1;
//   short a2;
//   int a3;
//   int* a4;  
// };

// int main()
// {
//     cout << sizeof(A) << endl;


//     return 0;
// }


class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* newhead = new ListNode(0);
        int t =0;
        ListNode* cur1 = l1;
        ListNode* cur2 = l2;
        ListNode* tail = newhead;
        while(cur1 || cur2 || t)
        {
            if(cur1)
            {
                t += cur1->val;
                cur1 = cur1->next;
            }
            if(cur2)
            {
                t += cur2->val;
                cur2 = cur2->next;
            }

            tail->next = new ListNode(t %10);
            tail = tail->next;
            t /= 10;
        }

        tail = newhead->next;
        delete newhead;

        return tail;
    }
};